package net.xinqushi.pthread.pthread;

public class ThreadBlocker {

    public static Thread main;
    public static boolean flag;
    public static void main(String[] args) {
        main = Thread.currentThread();
        ThreadBlocker thread = new ThreadBlocker();
        new Thread(()->{
            thread.lock1();
        }).start();
        try {
            Thread.sleep(3000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        flag = true;
        thread.notifyO(); //notify one thread in the monitor queue
        System.out.println(Thread.currentThread()+" will die");
    }

    synchronized void lock1() {
        try {
            System.out.println(this.getClass().getDeclaredMethod("lock1") + " is invoked");
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        try {
            while(true) {
                if (!flag) {
                    wait();//Block
                    System.out.println(Thread.currentThread()+" is notified");
                }
                else{
                    System.out.println(Thread.currentThread()+" will die");
                    break;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    synchronized void notifyO(){
        notifyAll();
    }
}
